import { defineStore } from 'pinia';
import { store } from '@/store';
import { SceneValuesEnum } from '@/enums/sceneValuesEnum';
import { CacheEnum } from '@/enums/CacheEnum';

export interface ITabbarItem {
  index: number;
  pagePath: string; //傀儡页 路径
  component: string;
  h5Link?: string; // H5外链地址
  text: string; //标题
  iconPath: string; //图标地址
  selectedIconPath: string; //选中图标地址
  hump?: boolean; //是否为凸起图标
  num?: number; //角标数量
  isDot?: boolean; //角标是否为圆点
  verify?: boolean; //是否验证  （如登录）
  // 跳转小程序的配置
  miniprogramOptions?: {
    appId: string;
    path?: string;
  };
  to?: () => void;
}

export interface AppState {
  tabbarList: ITabbarItem[];
  systemInfo: UniApp.GetSystemInfoResult | null;
  /** 自定义header 是否显示 */
  isHeaderShow: boolean;
  /** 显示/隐藏 隐私协议弹窗 */
  privacyPopupVisible: boolean;
  /** 隐私协议名称 */
  privacyContractName: string;
  /** 未同意隐私协议触发的回调resolve函数 */
  resolvePrivacyAuthorization: ((...args: any[]) => any) | null;
  /** 是否需要更新首页配置 */
  isUpdateHomeOptions: boolean;
  /** logo */
  logo: string;
  /** 小程序名称 */
  appName: string;
}

const createState = (): AppState => ({
  tabbarList: [],
  systemInfo: null,
  isHeaderShow: true,
  privacyPopupVisible: false,
  privacyContractName: '隐私保护指引',
  resolvePrivacyAuthorization: null,
  isUpdateHomeOptions: false,
  logo: 'https://hb-saas.obs.cn-south-1.myhuaweicloud.com/saas-ai/logo.png',
  appName: 'AI小邦'
});

export const useAppStore = defineStore({
  id: 'app',
  state: () => createState(),
  getters: {},
  actions: {
    /** 查询隐私授权情况 */
    getPrivacySetting() {
      return new Promise((resolve, reject) => {
        // #ifdef MP-WEIXIN
        const enterOptions = uni.getEnterOptionsSync();
        if (enterOptions.scene === SceneValuesEnum.TIMELINE_SINGLEPAGE) return resolve(true);
        if (wx.getPrivacySetting) {
          wx.getPrivacySetting({
            success: (res) => {
              this.privacyContractName = res.privacyContractName;
              res.needAuthorization && this.setPrivacyPopupVisible(true);
              res.needAuthorization ? resolve(false) : resolve(true);
            },
            fail: (err) => {
              console.log(err)
              reject(err)
            },
          });
        } else {
          resolve(true);
        }
        // #endif
        // #ifndef MP-WEIXIN
        resolve(true);
        // #endif
      });
    },
    /** 显示隐私弹窗 */
    setPrivacyPopupVisible(bol: boolean) {
      this.privacyPopupVisible = bol;
    },
    /** 设置隐私回调 */
    setResolvePrivacyAuthorization(fnc: (...args: any[]) => any) {
      this.resolvePrivacyAuthorization = fnc;
    },
    /**
     * @description 判断是否是pc端微信
     * @date 08/12/2022
     * @returns {boolean}
     */
    isPc(): Boolean {
      const pc = ['mac', 'windows'];
      return pc.includes(this.getSystemInfo().platform);
    },

    /**
     * @description 显示自定义头
     * @date 27/03/2023
     */
    showHeader() {
      this.isPc() && (this.isHeaderShow = true);
    },
    /**
     * @description 隐藏自定义头
     * @date 27/03/2023
     */
    hideHeader() {
      this.isPc() && (this.isHeaderShow = false);
    },

    /**
     * @description 获取系统信息
     * @date 16/11/2022
     * @returns {UniApp.GetSystemInfoResult}
     */
    getSystemInfo(): UniApp.GetSystemInfoResult {
      if (!this.systemInfo) {
        this.systemInfo = uni.getSystemInfoSync();
        console.log('systemInfo ====>', this.systemInfo);
      }
      return this.systemInfo;
    },

    /**
 * @description 获取小程序基础信息
 * @date 11/08/2022
 */
    getAccountInfoSync() {
      // #ifdef MP
      const { miniProgram } = uni.getAccountInfoSync();
      uni.setStorageSync(CacheEnum.APPID, miniProgram.appId);
      return miniProgram
      // #endif
      // #ifndef MP
      return {}
      // #endif
    },
  },
});

export function useAppStoreWithOut() {
  return useAppStore(store);
}
